package icangyu.base.views;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

import java.util.LinkedList;
import java.util.Queue;

public class LiveEnterTextView extends TextView {

    private static int SHOWTIME = 3000;
    private static int DURATION = 500;
    private static int FACTOR = 20;//factor*数量大小, 减少显示时间, 感觉应该做成指数类型

    private Queue<String> nameList = new LinkedList<>();
    private boolean isShowing = false;

    private AnimationSet enter,exit;

    Handler handler = new Handler(msg -> {
        if(msg.what == 100) {
            updateText();
        }
        return false;
    });

    public LiveEnterTextView(Context context) {
        super(context);
    }

    public LiveEnterTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public LiveEnterTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public LiveEnterTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void showName(String name){
        nameList.add(name);
//        handler.sendEmptyMessage(100);
        //在显示|未显示
        if(!isShowing && !handler.hasMessages(100)){
            handler.sendEmptyMessage(100);
        }
    }

    private void updateText(){
        if(!nameList.isEmpty()){
            setText(nameList.poll());
            setVisibility(VISIBLE);
            if(!isShowing){
                showEnterAnim();
            }
            isShowing = true;
            //这个时间应该由增加人数的时间间隔决定
            handler.sendEmptyMessageDelayed(100,SHOWTIME-FACTOR*nameList.size());
        }else {
            if(isShowing){
                showHideAnim();
            }
            setVisibility(GONE);
            isShowing = false;
        }
    }

    private void showEnterAnim(){
        if(enter == null){
            enter = new AnimationSet(false);
            AlphaAnimation alpha = new AlphaAnimation(0f, 1f);
            alpha.setInterpolator(new DecelerateInterpolator());
            TranslateAnimation trans = new TranslateAnimation(Animation.RELATIVE_TO_SELF,-1f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF, 0f,Animation.RELATIVE_TO_SELF, 0f);
            trans.setInterpolator(new DecelerateInterpolator());
            enter.addAnimation(alpha);
            enter.addAnimation(trans);
            enter.setDuration(DURATION);
            enter.setFillAfter(true);
        }
        clearAnimation();
        startAnimation(enter);
    }

    private void showHideAnim(){
        if(exit == null){
            exit = new AnimationSet(false);
            AlphaAnimation alpha = new AlphaAnimation(1f, 0f);
            alpha.setInterpolator(new DecelerateInterpolator());
            TranslateAnimation trans = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,-1f,Animation.RELATIVE_TO_SELF, 0f,Animation.RELATIVE_TO_SELF, 0f);
            trans.setInterpolator(new DecelerateInterpolator());
            exit.addAnimation(alpha);
            exit.addAnimation(trans);
            exit.setDuration(DURATION);
            exit.setFillAfter(true);
        }
        clearAnimation();
        startAnimation(exit);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        handler.removeCallbacksAndMessages(null);
    }
}
